package com.bing.util;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import java.util.List;
import java.util.Properties;

/**
 * Created by Administrator on 2014/6/22.
 * 旺旺聊天
 */
public class WW {
    private final static Logger logger = LoggerFactory.getLogger(WW.class);

    @Autowired
    @Qualifier("talks")
    private Properties talks;

    public static void login(WebDriver driver) {
        By wwId = By.cssSelector(".tstart-areas .tstart-custom-item .tstart-tdog-offline");
        try {
            driver.findElement(wwId).click();
            logger.info("点击登陆");
            By ok = By.cssSelector(".tdog-systips-content .tstart-tips-login-ok-btn");
            TaoBao.getWait(driver).until(ExpectedConditions.visibilityOfElementLocated(ok));
            driver.findElement(ok).click();
            logger.info("点击确定登陆");
            TaoBao.getWait(driver).until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".tstart-areas .tstart-custom-item .tstart-tdog-online")));
            logger.info("登陆成功");
        } catch (Exception e) {
            logger.info("己登陆");
        }
    }

    public static void talk(WebDriver driver, int mins) {
        boolean flag = true, waitReply = true;
        int i = 1;
        WebDriverWait wait = TaoBao.getWait(driver);
        long startTime = System.currentTimeMillis(), endTime;
        while (flag) {
            List<WebElement> elementList;
            while (waitReply) {
                elementList = driver.findElements(By.cssSelector(".tdog-popup-talkcontainer .tdog-popup-talkhistory .tdog-talk-others"));
                if (elementList.size() >= i) {
                    elementList.get(i - 1).getText();
                    logger.info("卖家回复:{},", elementList.get(i - 1).getText());
                    i++;
                } else {
                    logger.debug("卖家未回复,继续等待......");
                    TaoBao.pauseLong();
                }
                endTime = System.currentTimeMillis();
                if ((endTime - startTime) > mins * 1000 * 60) {
                    waitReply = false;
                    flag = false;
                    logger.info("聊天时间到,结束");
                }
            }

        }
    }
}
